package org.liveboardgames.agricola.business.action.processor;

import java.util.List;
import java.util.Map;

import org.liveboardgames.agricola.business.updater.CardUpdater;
import org.liveboardgames.agricola.business.updater.FarmUpdater;
import org.liveboardgames.agricola.domain.Change;
import org.liveboardgames.agricola.domain.Game;
import org.liveboardgames.agricola.domain.action.Action;
import org.liveboardgames.agricola.domain.card.Card;
import org.liveboardgames.agricola.domain.common.Target;
import org.liveboardgames.agricola.domain.player.Player;
import org.liveboardgames.agricola.model.enums.ResourceTypeEnum;

public class ResourceActionProcessor extends ActionProcessor {

  private String resourceType;

  private int outputQuantity;

  public ResourceActionProcessor(Game game, Action action, List<Change> changes, String resourceType, int outputQuantity) {
    super(game, action, changes);
    this.resourceType = resourceType;
    this.outputQuantity = outputQuantity;
  }

  @Override
  public void execute() {
    Target target = action.getTarget();
    if (target instanceof Card) {
      Card card = (Card)target;
      new CardUpdater(card, changes).addResource(resourceType, outputQuantity);
    }
    else if (target instanceof Player) {
      // This was introduced to handle the Animals card for 5 players, but
      // design looks strange
      Player player = (Player)target;
      Map<String, Integer> cost = action.getCost();
      FarmUpdater farmUpdater = new FarmUpdater(game, player.getFarm(), changes);
      farmUpdater.pay(cost);

      if (ResourceTypeEnum.ANIMALS.contains(resourceType)) {
        // new AnimalActionProcessor(action, resourceType,
        // outputQuantity).execute();
        throw new IllegalArgumentException("Not implemented yet");
      }
      else {
        farmUpdater.addResource(resourceType, outputQuantity);
      }
    }
    else {
      throw new IllegalArgumentException("not supported target " + target + " for type " + resourceType);
    }
  }
}
